/**
 * 
 */
package com.wolfware.athsweb.persistancy.springhibernate;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.wolfware.athsweb.persistancy.GenericDAO;

/**
 * @author cstrzadala
 * 
 */
@Repository
public abstract class AbstractSpringHibernateGenericDAOImpl<ID extends Number, E> extends HibernateDaoSupport implements
        GenericDAO<ID, E> {

    /**
     * Entity type. Define the entity type for the DAO.
     */
    private final Class<E> entityType;

    /**
     * Constructor.
     * 
     * @param entityType
     *            param
     */
    protected AbstractSpringHibernateGenericDAOImpl(final Class<E> entityType) {
        super();
        this.entityType = entityType;
    }

    /**
     * Create.
     * 
     * @param entity
     *            param
     */
    public void create(final E entity) {
        try {
            getHibernateTemplate().saveOrUpdate(entity);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in create");
            throw ex;
        }
    }

    /**
     * Read.
     * 
     * @param id
     *            param
     * @return val
     */
    @SuppressWarnings("unchecked")

    public E read(final ID id) {
        try {
            return (E) getHibernateTemplate().get(entityType, id);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in read");
            throw ex;
        }
    }

    /**
     * Update.
     * 
     * @param entity
     *            param
     */
    public void update(final E entity) {
        try {
            getHibernateTemplate().saveOrUpdate(entity);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in saveOrUpdate");
            throw ex;
        }
    }

    /**
     * Delete.
     * 
     * @param entity
     *            param
     */
    public void delete(final E entity) {
        try {
            getHibernateTemplate().delete(entity);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in delete");
            throw ex;
        }
    }

    /**
     * Find all.
     * 
     * @return list
     */
    @SuppressWarnings("unchecked")
    public List<E> findAll() {
        try {
            return (List<E>) getHibernateTemplate().loadAll(entityType);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in loadAll");
            throw ex;
        }
    }

    /**
     * Find by example.
     * 
     * @param example
     *            param
     * @return list
     */
    @SuppressWarnings("unchecked")
    public List<E> findByExample(E example) {
        try {
            return (List<E>) getHibernateTemplate().findByExample(example);
        } catch (DataAccessException ex) {
            System.out.println("DAO Exception in findByExample");
            throw ex;
        }
    }
}